package org.csu.mall.controller;

import org.csu.mall.common.CONSTANT;
import org.csu.mall.common.UniformResponse;
import org.csu.mall.entity.Shipping;
import org.csu.mall.service.IShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/shipping/")
public class ShippingController {
    @Autowired
    IShippingService shippingService;

    @PostMapping("add_shipping")
    @ResponseBody
    public UniformResponse<Shipping> addShipping(Shipping shipping, HttpSession session){
        UniformResponse<Shipping> response = shippingService.addShipping(shipping);
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.SHIPPING,response.getData());
        }
        return response;
    }
    @PostMapping("delete_shipping")
    @ResponseBody
    public UniformResponse<String> deleteShipping(String id){
        UniformResponse<String> response = shippingService.deleteShipping(id);
        return response;
    }
    @GetMapping("get_shipping_info_one")
    @ResponseBody
    UniformResponse<Shipping> getShippingInfoOne(String id,HttpSession session){
        UniformResponse<Shipping> response = shippingService.getShippingInfoOne(id);
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.SHIPPING,response.getData());
        }
        return response;
    }
    @GetMapping("get_shipping_info")
    @ResponseBody
    UniformResponse<List<Shipping>> getShippingInfo(HttpSession session){
        UniformResponse<List<Shipping>> response = shippingService.getShippingInfo();
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.SHIPPINGList,response.getData());
        }
        return response;
    }
    @PostMapping("update_shipping")
    @ResponseBody
    public UniformResponse<Shipping> updateShipping(Shipping shipping,HttpSession session){
        UniformResponse<Shipping> response = shippingService.updateShipping(shipping);
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.SHIPPING,response.getData());
        }
        return response;
    }
}
